package com.redlion.simulation;

import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.util.debug.Debug;
import org.andengine.util.math.MathUtils;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.redlion.GameScene;
import com.redlion.ObjState;
import com.redlion.GameActivity;
import com.redlion.object.Comet;
import com.redlion.object.Planet;

public class MyContactListener implements ContactListener {
	private String TAG = "Contact";
	private GameScene mEngineTestActivity;
	private static StringBuilder sb = new StringBuilder();

	public MyContactListener(GameScene engineTestActivity) {
		this.mEngineTestActivity = engineTestActivity;
	}

	@Override
	public void preSolve(Contact contact, Manifold oldManifold) {
	}

	@Override
	public void postSolve(Contact contact, ContactImpulse impulse) {
	}

	@Override
	public void endContact(Contact contact) {
		Debug.d(TAG, print(contact));
		if (contact.getFixtureA().getBody().isBullet()) {
			if (contact.getFixtureB().getBody().getUserData() != null) {
				if (contact.getFixtureB().getBody().getUserData().getClass().equals(Comet.class)) {
					Comet c = (Comet) contact.getFixtureB().getBody().getUserData();
					// mEngineTestActivity.createExplosion(Vector2Pool.obtain(contact.getFixtureB().getBody().getPosition()).mul(
					// GameController.METER_TO_PIXEL_RATIO));
					mEngineTestActivity.createExplosion(c.sprite);
					contact.getFixtureB().getBody().setUserData(ObjState.REMOVE);
				}
				contact.getFixtureA().getBody().setUserData(ObjState.REMOVE);
				return;
			}
		}
		if (contact.getFixtureB().getBody().isBullet()) {
			if (contact.getFixtureA().getBody().getUserData() != null) {
				if (contact.getFixtureA().getBody().getUserData().getClass().equals(Comet.class)) {
					Comet c = (Comet) contact.getFixtureA().getBody().getUserData();
					mEngineTestActivity.createExplosion(c.sprite);
					contact.getFixtureA().getBody().setUserData(ObjState.REMOVE);
				}
				contact.getFixtureB().getBody().setUserData(ObjState.REMOVE);
				return;
			}
		}
		Debug.d(TAG, contact.getWorldManifold().getNormal().toString());
		if (contact.getFixtureA().getBody().getUserData() != null && contact.getFixtureB().getBody().getUserData() != null) {
			if (contact.getFixtureA().getBody().getUserData().equals(Planet.class)) {
				if (contact.getFixtureB().getBody().getUserData().getClass().equals(Comet.class)) {
					Comet c = (Comet) contact.getFixtureB().getBody().getUserData();
					c.userData(ObjState.REMOVE);
					mEngineTestActivity.createImpact(c.sprite);
					return;
				}
			}
			if (contact.getFixtureB().getBody().getUserData().equals(Planet.class)) {
				if (contact.getFixtureA().getBody().getUserData().getClass().equals(Comet.class)) {
					Comet c = (Comet) contact.getFixtureA().getBody().getUserData();
					c.userData(ObjState.REMOVE);
					mEngineTestActivity.createImpact(c.sprite);
					return;
				}
			}
		}
	}

	private void printImpact(Vector2 planet, Vector2 comet) {
		sb.setLength(0);
		sb.append("Planet").append(planet.toString());
		sb.append(" Komet").append(comet.toString());
		Vector2 temp = Vector2Pool.obtain(comet).sub(planet);
		sb.append(" Temp").append(temp.toString());
		float angle = MathUtils.radToDeg(MathUtils.atan2(temp.x, temp.y));
		sb.append(" W:").append(angle);
		Debug.d(TAG, sb.toString());
	}

	private String print(Contact contact) {
		sb.setLength(0);
		sb.append("FixtureA: ");
		if (contact.getFixtureA().getBody().getUserData() != null)
			sb.append(contact.getFixtureA().getBody().getUserData());
		else
			sb.append("No User Data");
		sb.append(contact.getFixtureA().getBody().getPosition());
		sb.append("FixtureB: ");
		if (contact.getFixtureB().getBody().getUserData() != null)
			sb.append(contact.getFixtureB().getBody().getUserData());
		else
			sb.append("No User Data");
		sb.append(contact.getFixtureB().getBody().getPosition());
		return sb.toString();
	}

	@Override
	public void beginContact(Contact contact) {
	}
}